//
//  ViewController.m
//  17-UI-AddressBook
//
//  Created by Snow on 16/8/5.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "MyTableViewCell.h"
#import "DetailViewController.h"
#import "Model.h"
#import "AppDelegate.h"
#import "AddViewController.h"
#define kReuse @"cell"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *arrayOfModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  使用tableView完成通讯录, 注意联系人使用类创建
     添加联系人 联系人包含 头像 姓名 联系方式 家庭住址
     联系人列表 (注意: 添加完联系人, 列表要刷新)
     删除联系人
     查看联系人详情
     */
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleAdd:)];
    _arrayOfModel = [NSMutableArray array];
    NSString *path = @"/Users/Snow/DLS160710/UI/HomeWork/17-UI-AddressBook/17-UI-AddressBook/DLS160710.plist";
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in array) {
        Model *model = [[Model alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_arrayOfModel addObject:model];
    }
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:kReuse];
    [_tableView reloadData];
}
- (void)handleAdd:(UIBarButtonItem *)button
{
    AddViewController *vc = [[AddViewController alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_arrayOfModel removeObjectAtIndex:indexPath.row];
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayOfModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    Model *model = [_arrayOfModel objectAtIndex:indexPath.row];
    cell.labelOfName.text = model.name;
    cell.labelOfPhone.text = model.phoneNumber;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *vc = [[DetailViewController alloc] init];
    vc.model = [_arrayOfModel objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)addContact:(Model *)model
{
    [_arrayOfModel addObject:model];
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
